package org.tissou.client.Panel_Premier.operations_tab.Operations;

import org.tissou.client.Interface.Base_Panel;
import org.tissou.client.Services.*;
import org.tissou.client.widgets.Label_MessageInformation;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.datepicker.client.DateBox;
import com.google.gwt.i18n.client.DateTimeFormat;

import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 * User: Claire
 * Date: 31-Mar-2009
 * Time: 08:27:49
 * To change this template use File | Settings | File Templates.
 */
public abstract class Operation_Base extends Base_Panel{
  DockPanel principal = new DockPanel();
  Label_MessageInformation message_information = new Label_MessageInformation();
  DateBox datePicker = new DateBox();
  Label date_Label = new Label("Date: ");

  Timer t = new Timer(){
      public void run() {
        PasDeMessage();
      }
  };

  public Operation_Base() {
    datePicker.setFormat(new DateBox.DefaultFormat(DateTimeFormat.getFormat("dd/MM/yyyy")));
  }


  public abstract Parametres FormerParametresOperation();
  public abstract void InitialiserValeurs();

  public void EnvoyerInformationServeur(Parametres p){
    MessageAttente();
    final AsyncCallback c = new AsyncCallback(){
      public void onFailure(Throwable caught) {
        System.out.println("Entrer Transaction: Ca a pas marche pas de chance");
        MessageErreurServeur(caught.getLocalizedMessage());
      }

      public void onSuccess(Object result) {
        System.out.println("Entrer Transaction: Congratulations Petite Clairette, You are the best of the world !");
        MessageReussi();
        InitialiserValeurs();
      }
    };
    EntrerTransactionService.App.getInstance().EntrerTransaction(p,c);
  }

  public ArbreDeComptes CreerArbreCompteAvecTreeListener(ProprietaireInfos p){
    ArbreDeComptes arbre = new ArbreDeComptes(p.getNom());
        arbre.addTreeListener(new TreeListener(){
          public void onTreeItemSelected(TreeItem item) {
            DonnerCompte(item);
          }

          public void onTreeItemStateChanged(TreeItem item) {

          }
        });
    return arbre;
  }

  public Compte DonnerCompte(TreeItem item) {
      Compte c;
        try {
          c = (Compte)item.getUserObject();
        }
        catch(Exception e){
          c = null;
        }
    return c;
  }

  public void MessageReussi(){
    message_information.setMessage(Label_MessageInformation.Etat.SERVICEMARCHE, "L'operation est rentree ! Tu geres mec.");
    t.schedule(3000);
  }

  public void MessageErreurEntree(String s){
    message_information.setMessage(Label_MessageInformation.Etat.MAUVAISEENTREE,s);
  }

  public void MessageErreurServeur(String s){
    message_information.setMessage(Label_MessageInformation.Etat.SERVICEPASMARCHE,s);
  }

  public void PasDeMessage(){
    message_information.setMessage(Label_MessageInformation.Etat.NEUTRE, "");
  }

  public void MessageAttente(){
    message_information.setMessage(Label_MessageInformation.Etat.SERVICEATTENTE, "En attente d'une reponse du serveur");
  }



  public ProprietaireInfos DonnerProprietaire(TreeItem item) {
      ProprietaireInfos p;
        try {
          p = (ProprietaireInfos)item.getUserObject();
        }
        catch(Exception e){
          p = null;
        }

    return p;
  }

  protected Date getDate(){
    return datePicker.getValue();
  }

  protected Panel donneLePanelPrincipal() {
    return principal;
  }
}
